package com.zhilianbao.leyaogo.view.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import butterknife.ButterKnife;

/**
 * ClassName: BasePopup <br>
 * Description: Popup基类<br>
 * Author: Cyarie <br>
 * Created: 2016/10/12 11:29 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public abstract class BasePopup extends PopupWindow {

    public PopupWindow mPopupWindow;
    public Activity mActivity;


    public BasePopup(Activity activity) {
        this.mActivity = activity;
        initPopupView();
    }


    public abstract int getLayoutResId();

    public abstract void onCreatePopupView(View view);


    public void initPopupView() {
        View contentView = View.inflate(mActivity, getLayoutResId(), null);
        mPopupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0));
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss() {
                WindowManager.LayoutParams attributes = mActivity.getWindow().getAttributes();
                attributes.alpha = 1.0f;
                mActivity.getWindow().setAttributes(attributes);
            }
        });
        ButterKnife.bind(this, contentView);
        onCreatePopupView(contentView);
    }


    public void show(View view) {
        if (mPopupWindow != null) {
            WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
            lp.alpha = 0.5f;
            mActivity.getWindow().setAttributes(lp);
            mPopupWindow.showAsDropDown(view);
        }
    }
}
